#@layout("我的RSS") 

#define main()
<fieldset class="layui-elem-field">
	<legend>我的RSS</legend>
	<div style="width: 120em; overflow: auto;">
		<div class="layui-field-box layui-form">
			<table class="layui-table admin-table">
				<thead>
					<tr>
						<th style="width: 30px;"><input type="checkbox"
							lay-filter="allselector" lay-skin="primary"></th>
						<th>频道</th>
						<th>标题</th>
						<th>作者</th>
						<th>发布时间</th>
						<th>类型</th>
						<th>操作</th>
					</tr>
				</thead>
				<tbody id="content">
				</tbody>
				<tfoot>
					<tr>
						<th colspan="7 ">
							<div class="admin-table-page">
								<div id="paged" class="page"></div>
							</div>
						</th>
					</tr>
				</tfoot>
			</table>
		</div>
	</div>
</fieldset>
#end
 
#define searchForm()
			<blockquote class="layui-elem-quote">
				<form class="layui-form layui-box"  style="margin-top: 1.5em;" id="search-form">
			
				<div class="layui-form-item">
					<label class="layui-form-label">频道</label>
					<div class="layui-input-inline">
						<select name="channel_id" id="channel_id" lay-verify="" lay-search>
						  <option value="">请选择频道(可搜索)</option>
						</select> 
					</div>
					<label class="layui-form-label">标题</label>
					<div class="layui-input-inline">
						 <input type="text" name="title"  lay-verify="" placeholder="标题" autocomplete="off" class="layui-input">
					</div>
					<label class="layui-form-label">发布日期</label>
					<div class="layui-input-inline">
						 <input type="text" name="pubdate"  placeholder="发布日期" autocomplete="off" class="layui-input" onclick="layui.laydate({elem: this})">
					</div>
					<div class="layui-input-inline">
					<button  lay-submit lay-filter="search" class="layui-btn layui-btn" >
						<i class="layui-icon">&#xe615;</i>搜索
					</button>
					<a href="javascript:;" class="layui-btn layui-btn" id="url-add">
						<i class="fa fa-shopping-cart" aria-hidden="true"></i>创建
					</a>
					</div>
				</div>
				</form>
			</blockquote>
#end

#define js()
<!--模板-->
<script type="text/html" id="tpl">
			{{# layui.each(d.list.list, function(index, item){ }}
			<tr>
				<td><input type="checkbox" lay-skin="primary"></td>
				<td>{{ item.name}}</td>
				<td>{{ item.title }}</td>
				<td>{{ item.author }}</td>
				<td>{{ item.pubdate }}</td>
				<td>{{ item.type }}</td>
				<td><a href="javascript:;" data-name='{{ item.id }}' data-opt="del" class="layui-btn layui-btn-mini">删除</a></td>
			</tr>
			{{# }); }}
		</script>
<script type="text/javascript">

			layui.config({
				base: 'js/'
			});
			layui.use(['paging', 'form', 'laydate', 'layedit','upload'], function() {
			var $ = layui.jquery,
				paging = layui.paging(),
				layerTips = parent.layer === undefined ? layui.layer : parent.layer, //获取父窗口的layer对象
				layer = layui.layer, //获取当前窗口的layer对象
				layedit = layui.layedit,
				form = layui.form();
			
			//表单赋值
			//获取channel的list
			var channels = "";
			 $.ajax({
					type: 'post',
					url:  'cha/channels$list',
					async: false,
					data: '',
					dataType: 'json',
					success: function(ret, status, xhr) {
					if(ret.isOk){
						channels = ret.list;
					}else{
						layerTips.msg(ret.msg);
					}
					}
			 });
			
			 
			 $(channels).each(function(i, item){
				 $("#channel_id").append('<option value="'+item.id+'">'+item.name+'</option>');
			 });
			 
			 form.render();
			 
			form.on('submit(search)', function(data){
				
				loadtab(data.field);
				
				return false; 
			});
			var _data = {};
			var loadtab = function(data){ 
			paging.init({
				url: 'rss/mychannels',  //地址
				elem: '#content', //内容容器
				params: data,
				type: 'post',
				tempElem: '#tpl', //模块容器
				pageConfig: { //分页参数配置
					elem: '#paged', //分页容器
					pageSize: 10 //分页大小
				},
				success: function() { //渲染成功的回调
					//alert('渲染成功');
				},
				fail: function(msg) { //获取数据失败的回调
					//alert('获取数据失败')
				},
				complate: function() { //完成的回调
					//alert('处理完成');
					//重新渲染复选框
					form.render('checkbox');
					form.on('checkbox(allselector)', function(data) {
						var elem = data.elem;

						$('#content').children('tr').each(function() {
							var $that = $(this);
							//全选或反选
							$that.children('td').eq(0).children('input[type=checkbox]')[0].checked = elem.checked;
							form.render('checkbox');
						});
					});

					//绑定所有edit按钮事件						
					$('#content').children('tr').each(function() {
						var $that = $(this);
						$that.children('td:last-child').children('a[data-opt=edit]').on('click', function() {
							var data = $(this).data('name');
							//alert(data);
							//rss订阅
							});
					});
					$('#content').children('tr').each(function() {
						var $that = $(this);
						$that.children('td:last-child').children('a[data-opt=del]').on('click', function() {
							var cid = $(this).data('name');
							 $.ajax({
									type: 'post',
									url:  'rss/del',
									async: false,
									data: {id: cid},
									dataType: 'json',
									success: function(ret, status, xhr) {
										if(ret.isOk){
											layerTips.msg(ret.msg);
											location.reload();
										}else{
											layerTips.msg(ret.msg);
										}
										layer.close(index);
								}
						     });
							});
					});
				}
			});
			};
			loadtab(_data);
			
			
			/**
			 * rss url 导入
			 */
			$('#url-add').on('click', function() {
			
				$.get('htmlTemp/addRss.html', null, function(form) {
					addBoxIndex = layer.open({
						type: 1,
						title: '创建我的RSS',
						content: form,
						btn: ['确定', '取消'],
						shade: false,
						offset: ['100px', '30%'],
						area: ['600px', '450px'],
						zIndex: 19950924,
						yes: function(index) {
							$('form.layui-form').find('button[lay-filter=edit]').click();
						},
						success: function(layero, index) {
							//channels$list
							
							 $(channels).each(function(i, item){
								 
								 $("#channel").append('<option value="'+item.id+'">'+item.name+'</option>');
							 });
							//创建一个编辑器
							var layedit = layui.layedit;
						    var editIndex = layedit.build('LAY_demo_editor');
							var form = layui.form();
							form.render();
							form.on('submit(edit)', function(data) {
								delete data.field.file;
								 $.ajax({
										type: 'post',
										url:  'rss/add',
										async: false,
										data: data.field,
										dataType: 'json',
										success: function(ret, status, xhr) {
											if(ret.isOk){
												layerTips.msg(ret.msg);
												location.reload();
											}else{
												layerTips.msg(ret.msg);
											}
											layer.close(index);
									}
							     });
								
							});
							}
						});
					});	
				});	
			});
			
		</script>
#end

